package com.mall.server.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.utils.PageUtils;
import com.mall.dubbo.api.UmsRoleApi;
import com.tanhua.model.domain.UmsMenu;
import com.tanhua.model.domain.UmsRole;
import com.mall.commons.exceotion.ErrorResult;
import com.tanhua.model.vo.PageVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UmsRoleService {

    @DubboReference
    private UmsRoleApi umsRoleApi;

    //获取所有角色
    public List<UmsRole> roleListAll() {

        return umsRoleApi.roleListAll();
    }

    //根据角色名称分页获取角色列表
    public PageVo roleList(String keyword, Integer pageNum, Integer pageSize) {
        Page page = umsRoleApi.roleList(keyword, pageNum, pageSize);
        PageVo pageDto = PageUtils.getPageVo(page);
        return pageDto;
    }

    //获取角色相关菜单
    public List<UmsMenu> listMenu(Integer roleId) {
        if (roleId == null) {
            return null;
        }
        List<UmsMenu> umsRole = umsRoleApi.listMenu(roleId);
        /*if (umsRole == null) {
            throw new BusinessException(ErrorResult.emptyError());
        }*/
        return umsRole;
    }

    //给角色分配菜单
    public void allocMenu(Integer roleId, Integer[] menuIds) {
        boolean exist = umsRoleApi.allocMenu(roleId, menuIds);
        if (!exist) {
            throw new BusinessException(ErrorResult.emptyError());
        }
    }

    //添加角色
    public void create(UmsRole umsRole) {
        boolean exist = umsRoleApi.create(umsRole);
        if (!exist) {
            throw new BusinessException(ErrorResult.emptyError());
        }
    }

    //批量删除角色
    public void deletes(Integer[] ids) {
        boolean exist = umsRoleApi.deletes(ids);
        if (!exist) {
            throw new BusinessException(ErrorResult.emptyError());
        }
    }

    //获取角色相关资源
    public UmsRole listResources(Integer roleId) {
        UmsRole exist = umsRoleApi.UmsRole(roleId);
        if (exist == null) {
            throw new BusinessException(ErrorResult.emptyError());
        }
        return exist;
    }

    //给角色分配资源
    public void allocResource(Integer roleId, Integer[] resourceIds) {
        boolean exist = umsRoleApi.allocResource(roleId, resourceIds);
        if (!exist) {
            throw new BusinessException(ErrorResult.emptyError());
        }
    }

    //修改角色状态
    public void updateStatus(Integer status, Integer id) {
        boolean exist = umsRoleApi.updateStatus(status, id);
        if (!exist) {
            throw new BusinessException(ErrorResult.emptyError());
        }
    }
}